﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Security.Principal;
namespace KALE_Scanning
{
    public sealed class UserProperty
    {
        private static UserProperty property = new UserProperty();
        private static PDFValues pdfValues = new PDFValues();
        private UserProperty() {
            
           
        }
        private UserProperty(string domain, string username, string password) {
            
            _nc = new System.Net.NetworkCredential();
            this.Domain = domain;
            this.UserName = username;
            this.Password = password;
            _nc.Domain = this.Domain;
            _nc.UserName = this.UserName;
            _nc.Password = this.Password;

        }
        private UserProperty(System.Net.NetworkCredential nc)
        {
            
            _nc = nc;
            this.Domain = nc.Domain;
            this.UserName = nc.UserName;
            this.Password = nc.Password;
        }

        public static void PrepareUserCredential()
        {
            
            string path = System.Windows.Forms.Application.StartupPath + "\\scanUpload.xml";
            if (!System.IO.File.Exists(path))
            {
                System.Windows.Forms.MessageBox.Show("User bilgileri bulunamadı. Lütfen Kullanıcı ayarları kısmında bilgilerinizi kaydediniz.");
                return;
            }
            else
            {
                XDocument xDoc = XDocument.Load(path);
                var oCre = (from credential in xDoc.Descendants("ScanUpload")
                            select new
                            {
                                Exist = credential.Element("Credential").Attribute("Exist").Value,
                                Username = credential.Element("Credential").Attribute("UserName").Value,
                                Password = credential.Element("Credential").Attribute("Password").Value,
                                Domain = credential.Element("Credential").Attribute("Domain").Value,
                                Url = credential.Element("Information").Attribute("Url").Value
                            }).FirstOrDefault();
                if (property == null)
                    property = UserProperty.UserProperties;
                property.Url = oCre.Url != string.Empty ? Utility.Coz(oCre.Url) : "";
                property.UserName = oCre.Username != string.Empty ? Utility.Coz(oCre.Username) : "";
                property.Password = oCre.Password != string.Empty ? Utility.Coz(oCre.Password) : "";
                property.Domain = oCre.Domain != string.Empty ? Utility.Coz(oCre.Domain) : "";
                if (oCre.Exist != string.Empty)
                {
                    property.Exist = Convert.ToBoolean(oCre.Exist);
                }
                else
                {
                    property.Exist = false;

                }
                if (oCre.Username == string.Empty || oCre.Password == string.Empty || oCre.Domain == string.Empty)
                {
                    MessageBox.Show("Kullanıcı bilgileri eksik. Lütfen ayarlar bölümünden ilgili verileri giriniz");
                    return;
                }
                if(property.Exist)
                    property.Credential = new System.Net.NetworkCredential(property.UserName, property.Password, property.Domain);
                else
                    property.Credential = System.Net.CredentialCache.DefaultNetworkCredentials;

                property.NetworkCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
                
            }
            
        }
        public static void SetPDFValues(string title, string subject, string keyword)
        {
            pdfValues.Title = title;
            pdfValues.Subject = subject;
            pdfValues.Keywords = keyword;
            pdfValues.IsClear = false;
        }
        public static void ClearPDFValues()
        {
            pdfValues.Title = "";
            pdfValues.Subject = "";
            pdfValues.Keywords = "";
            pdfValues.IsClear = true;
        }

        private System.Net.NetworkCredential _nc = null;
        public System.Net.NetworkCredential NetworkCredential { get; set; }
        public string Domain { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Url { get; set; }
        public bool Exist { get; set; }
        public System.Net.NetworkCredential Credential
        {
            get { return _nc; }
            set { _nc = value; }
            
        }


        public static UserProperty UserProperties
        {
            get {
                return property;
            }
        }

        public static PDFValues PDFValue
        {
            get { return pdfValues; }
        }

    }

    public class PDFValues
    {
        public PDFValues() { }
        public PDFValues(string title, string subject, string keywords)
        {
            this.Title = title;
            this.Subject = subject;
            this.Keywords = keywords;
        }
        public string Title { get; set; }
        public string Subject { get; set; }
        public string Keywords { get; set; }
        public bool IsClear { get; set; }
    }
}
